package com.yanqu.road.dao.impl.activity.seacraft;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotBattlePatrons;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotMonsterData;
import com.yanqu.road.entity.activity.seacraft.pirateriot.SeacraftPirateRiotUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SeacraftPirateRiotUserDataDaoImpl extends TempDataObjectDao<SeacraftPirateRiotUserData> {

    public static String getSeacraftPirateRiotUserDataSql = "select * from `t_u_seacraft_pirate_riot_user` where `activityId`=? and `userId`=?";

    public SeacraftPirateRiotUserData getSeacraftPirateRiotUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSeacraftPirateRiotUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<List<Integer>> getAllActivityChapter(long userId) {
        List<List<Integer>> listList = new ArrayList<>();
        listList.add(new ArrayList<>()); // 关卡ID
        listList.add(new ArrayList<>()); // 关卡状态

        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall("select chapterId,chapterStatus from `t_u_seacraft_pirate_riot_user` where `userId`=?");
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                int chapterId = rs.getInt("chapterId");
                int chapterStatus = rs.getInt("chapterStatus");
                listList.get(0).add(chapterId);
                listList.get(1).add(chapterStatus);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return listList;
    }


    public static String updateSeacraftPirateRiotUserDataSql = "update `t_u_seacraft_pirate_riot_user` set energy=?,lastRecTime=?,monsterManual=?,rewardMonster=?,score=?,equipLevel=?,battlePatronsMap=?,blockList=?,chapterId=?,chapterStatus=?,sectionId=?,monsterList=?,guideIndex=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(SeacraftPirateRiotUserData seacraftPirateRiotUserData) {
        seacraftPirateRiotUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getMonsterManual())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getRewardMonster())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getEquipLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getBattlePatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getBlockList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getChapterId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getChapterStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getSectionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getMonsterList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getGuideIndex()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSeacraftPirateRiotUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSeacraftPirateRiotUserDataSql = "insert into `t_u_seacraft_pirate_riot_user`(activityId,userId,energy,lastRecTime,monsterManual,rewardMonster,score,equipLevel,battlePatronsMap,blockList,chapterId,chapterStatus,sectionId,monsterList,guideIndex) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(SeacraftPirateRiotUserData seacraftPirateRiotUserData) {
        seacraftPirateRiotUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getLastRecTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getMonsterManual())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getRewardMonster())));
        para.put(i++, new DbParameter(Types.BIGINT, seacraftPirateRiotUserData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getEquipLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getBattlePatronsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getBlockList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getChapterId()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getChapterStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getSectionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(seacraftPirateRiotUserData.getMonsterList())));
        para.put(i++, new DbParameter(Types.INTEGER, seacraftPirateRiotUserData.getGuideIndex()));
        if (getDb().executeNoneQuery(insertSeacraftPirateRiotUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SeacraftPirateRiotUserData parseData(ResultSet rs) throws SQLException {
        SeacraftPirateRiotUserData seacraftPirateRiotUserData = new SeacraftPirateRiotUserData();
        seacraftPirateRiotUserData.setActivityId(rs.getInt("activityId"));
        seacraftPirateRiotUserData.setUserId(rs.getLong("userId"));
        seacraftPirateRiotUserData.setEnergy(rs.getInt("energy"));
        seacraftPirateRiotUserData.setLastRecTime(rs.getLong("lastRecTime"));
        seacraftPirateRiotUserData.setMonsterManual(JSON.parseObject(rs.getString("monsterManual"), new TypeReference<List<Integer>>() {
        }));
        seacraftPirateRiotUserData.setRewardMonster(JSON.parseObject(rs.getString("rewardMonster"), new TypeReference<List<Integer>>() {
        }));
        seacraftPirateRiotUserData.setScore(rs.getLong("score"));
        seacraftPirateRiotUserData.setEquipLevel(rs.getInt("equipLevel"));
        seacraftPirateRiotUserData.setBattlePatronsMap(JSON.parseObject(rs.getString("battlePatronsMap"), new TypeReference<ConcurrentHashMap<Integer, SeacraftPirateRiotBattlePatrons>>() {
        }));
        seacraftPirateRiotUserData.setBlockList(JSON.parseObject(rs.getString("blockList"), int[][].class));
        seacraftPirateRiotUserData.setChapterId(rs.getInt("chapterId"));
        seacraftPirateRiotUserData.setChapterStatus(rs.getInt("chapterStatus"));
        seacraftPirateRiotUserData.setSectionId(rs.getInt("sectionId"));
        seacraftPirateRiotUserData.setMonsterList(JSON.parseObject(rs.getString("monsterList"), new TypeReference<List<SeacraftPirateRiotMonsterData>>() {
        }));
        seacraftPirateRiotUserData.setGuideIndex(rs.getInt("guideIndex"));
        return seacraftPirateRiotUserData;
    }

}
